﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebApp.Data;

namespace WebApp.Commons
{
    public class DataProvider
    {
        public static List<tbl_country> GetCountry()
        {
            using (var db = new PAVINGDataContext())
            {
                return db.tbl_countries.ToList();
            }
        }

        public static int GetLastUserId()
        {
            using(var db = new PAVINGDataContext())
            {
                if (db.tbl_users.Count() == 0)
                    return 0;
                return db.tbl_users.Max(w => w.user_id);
            }
        }

        public static int GetLastCustomerId()
        {
            using (var db = new PAVINGDataContext())
            {
                if (db.tbl_customers.Count() == 0)
                    return 0;
                return db.tbl_customers.Max(w => w.customer_id);
            }
        }

        public static int GetLastProjectAreaId()
        {
            using (var db = new PAVINGDataContext())
            {
                if (db.tbl_project_areas.Count() == 0)
                    return 0;
                return db.tbl_project_areas.Max(w => w.project_area_id);
            }
        }

        public static string GetEmployeeTypeCode(int userTypeid)
        {
            using (var db = new PAVINGDataContext())
            {
                var item = db.tbl_user_types.SingleOrDefault(s => s.user_type_id.Equals(userTypeid));
                if (item == null)
                    return "";
                return item.user_type_code;
            }
        }

        public static List<tbl_material> GetMaterial()
        {
            using (var db = new PAVINGDataContext())
            {
                return db.tbl_materials.ToList();
            }
        }

        public static List<tbl_material_type> GetMaterialType()
        {
            using (var db = new PAVINGDataContext())
            {
                return db.tbl_material_types.ToList();
            }
        }

        public static List<tbl_step> GetStep()
        {
            using (var db = new PAVINGDataContext())
            {
                return db.tbl_steps.ToList();
            }
        }

        public static List<tbl_customer> GetCustomer()
        {
            using (var db = new PAVINGDataContext())
            {
                return db.tbl_customers.ToList();
            }
        }

        public static string GetAreaTypeNameById(int areaId)
        {
            using (var db = new PAVINGDataContext())
            {
                var item = db.tbl_area_types.SingleOrDefault(a => a.area_type_id == areaId);
                if (item == null)
                    return string.Empty;
                return item.area_type_name;
            }
        }
    }
}